import axios from "axios";
import React, { Component } from 'react'

let testPost = (param, url = `/api/graphql`) => {
  return fetch(url, {
    method: "POST",
    headers: {
      // "Content-Type": "application/json"
      "Content-Type": "application/x-www-form-urlencoded"
    },
    body: param
  });
};

export const netPost = (param, url = `/api/graphql`) => {
  return new Promise((resolve, reject) => {
    testPost(param, url)
      .then(response => response.json())
      .then(json => {
        if (json && !json.data) {
          resolve(json);
        } else if (json && json.data) {
          let res = null;
          for (var key in json.data) {
            let item = json.data[key];
            if (key && item) res = item;
            break;
          }
          resolve(res);
        } else {
          resolve(null);
        }
      })
      .catch(err => {
        resolve(null);
      });
  });
};

export let HOC = (WrappedComponent) => {
  return class HOCComponent extends Component {
    // constructor(){
    //   super(props)
    //   // this.props = 
    // }
    componentWillMount() {
      console.log("123 --- ", this);
    }
    render(){
      return <WrappedComponent {...this.props} />
    }
  }
}